package com.xbongbong.pro.statistic.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.xbongbong.enums.Priority;
import com.xbongbong.enums.ReleaseState;
import com.xbongbong.helper.ExecutorProcessHelper;
import com.xbongbong.helper.ProcessDefinitionHelper;
import com.xbongbong.helper.ProjectsHelper;
import com.xbongbong.helper.SchedulerHelper;
import com.xbongbong.helper.TaskDefinitionHelper;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.pojo.LocationPojo;
import com.xbongbong.pojo.ProcessTaskRelation;
import com.xbongbong.pojo.ScheduleParam;
import com.xbongbong.pojo.SqlParametersPojo;
import com.xbongbong.pojo.TaskDefinition;
import com.xbongbong.pro.domain.entity.DataSetConfigEntity;
import com.xbongbong.pro.domain.entity.DataSetDolphinEntity;
import com.xbongbong.pro.statistic.model.DataSetConfigModel;
import com.xbongbong.pro.statistic.model.DataSetDolphinModel;
import com.xbongbong.pro.statistic.result.pojo.dto.ProjectDTO;
import com.xbongbong.pro.statistic.result.pojo.dto.ProjectDataSetDTO;
import com.xbongbong.pro.statistic.service.DolphinConnService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 描述信息
 *
 * @ClassName DolphinConnServiceImpl
 * @Author 梁鲁江
 * 创建时间 2022/6/28 19:10
 * 修改时间  2022/6/28 19:10
 **/
@Service("dolphinConnService")
public class DolphinConnServiceImpl implements DolphinConnService {
    @Resource
    private ProjectsHelper dealProjects;
    @Resource
    private TaskDefinitionHelper taskDefinitionHelper;
    @Resource
    private ProcessDefinitionHelper processDefinitionHelper;
    @Resource
    private SchedulerHelper schedulerHelper;
    @Resource
    private DataSetDolphinModel dataSetDolphinModel;
    @Resource
    private DataSetConfigModel dataSetConfigModel;
    
    @Override
    public void createProcess(ProjectDataSetDTO projectDataSetDTO) throws XbbException {
        dealProjects.create(projectDataSetDTO.getProjectName(),"");
        ProjectDTO projectDTO = new ProjectDTO();
        projectDTO.setProjectName(projectDataSetDTO.getProjectName());
        //获取项目code
        Long projectCode = dealProjects.queryCodeByName(projectDTO);
        //生成TaskCode
        Long taskCode = taskDefinitionHelper.genTaskCode(projectCode);
        LocationPojo locationPojo  = new LocationPojo(taskCode);
        JSONArray locations = new JSONArray();
        locations.add(JSON.toJSON(locationPojo));
        //TODO 
        SqlParametersPojo sqlParametersPojo = new SqlParametersPojo();
        sqlParametersPojo.setType("CLICKHOUSE");
        //TODO 这个要设定好或者动态获取
        sqlParametersPojo.setDatasource(5);
        //动态解析之后传递过来
        sqlParametersPojo.setSql("insert into tb_paas_form_data_test_1  select * from tb_paas_form_data limit 1;");
        sqlParametersPojo.setSqlType("1");
        sqlParametersPojo.setSendEmail(false);
        sqlParametersPojo.setDisplayRows(10);
        sqlParametersPojo.setTitle("");
        JSONObject taskParams = JSONObject.parseObject(JSON.toJSONString(sqlParametersPojo));
        //TODO environmentCode 需要配置或者动态查
        TaskDefinition taskDefinition = new TaskDefinition(taskCode,"task测试接口集成测试1",taskParams, Priority.MEDIUM,5036601448992L);
        JSONArray taskDefinitionJson = new JSONArray();
        taskDefinitionJson.add(JSONObject.parseObject(JSON.toJSONString(taskDefinition)));
        //
        ProcessTaskRelation processTaskRelation = new ProcessTaskRelation(taskCode);
        JSONArray taskRelationJson = new JSONArray();
        taskRelationJson.add(JSONObject.parseObject(JSON.toJSONString(processTaskRelation)));
        //定义流程
        Long processCode = processDefinitionHelper.createProcessDefinition(projectCode, "DAG插入接口测试", "我是描述", "[]", locations.toJSONString(), 0, "root",
                taskRelationJson.toJSONString(),
                taskDefinitionJson.toJSONString()
        );
        //流程上线
        processDefinitionHelper.releaseProcessDefinition(projectCode,processCode, ReleaseState.ONLINE.getCode());

        //TODO  scheduleId 待接入，先设置页面定时任务
        ScheduleParam scheduleParam = new ScheduleParam(new Date("2022-06-27 00:00:00"),new Date("2022-06-27 00:00:00"),"Asia/Shanghai","* * * * * ? *");
        Integer scheduleId = schedulerHelper.insertSchedule(projectCode, processCode, JSON.toJSONString(scheduleParam));
        
        //记录到表中
        Long dataSetId = projectDataSetDTO.getDataSetId();
        String corpid = projectDataSetDTO.getCorpid();
        DataSetConfigEntity dataSetConfigEntity = dataSetConfigModel.getByDataSetId(dataSetId,corpid);
        DataSetDolphinEntity dataSetDolphinEntity = new DataSetDolphinEntity(corpid, dataSetId,dataSetConfigEntity.getId(),projectCode,processCode,taskCode,scheduleId,projectDataSetDTO.getUserId());
        dataSetDolphinModel.insert(dataSetDolphinEntity);
        
        //设置定时任务上线
        schedulerHelper.setScheduleState(projectCode,scheduleId,ReleaseState.ONLINE.getCode());
        
    }
}
